package ciyuan.Player.Skill;

import ciyuan.Constants.PlayerDataType;
import ciyuan.DataManager.PlayerDataManager;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Network.NetWorkRegister;
import ciyuan.Network.SkillCastPacket;
import ciyuan.Utils.KeybindingRegisterManager;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Vector;

@Mod.EventBusSubscriber(Dist.CLIENT)
public class SkillManager {

    private static final Vector<SkillBookType> skillList = new Vector<>();

    public static void addSkill(SkillBookType skill) {
        skillList.add(skill);
    }

    @SubscribeEvent
    public static void onKeyboardInput(InputEvent.KeyInputEvent event) {
        if (KeybindingRegisterManager.SKILL_KEY1.isDown()) {
            ItemStack itemStack = PlayerDataManager.getItemStack(null, PlayerDataType.SKILL_SLOT1);
            if (!itemStack.isEmpty() && itemStack.getItem() instanceof SkillBookType) {
                SkillBookType skill = (SkillBookType) itemStack.getItem();
                if (SkillUtils.canCastSkill(Minecraft.getInstance().player, skill)) {
                    NetWorkRegister.INSTANCE.sendToServer(new SkillCastPacket(PlayerDataType.SKILL_SLOT1.getName()));
                    skill.keyDown(Minecraft.getInstance().player);
                }
            }
        } else if (KeybindingRegisterManager.SKILL_KEY2.isDown()) {
            ItemStack itemStack = PlayerDataManager.getItemStack(null, PlayerDataType.SKILL_SLOT2);
            if (!itemStack.isEmpty() && itemStack.getItem() instanceof SkillBookType) {
                SkillBookType skill = (SkillBookType) itemStack.getItem();
                if (SkillUtils.canCastSkill(Minecraft.getInstance().player, skill)) {
                    NetWorkRegister.INSTANCE.sendToServer(new SkillCastPacket(PlayerDataType.SKILL_SLOT2.getName()));
                    skill.keyDown(Minecraft.getInstance().player);
                }
            }
        } else if (KeybindingRegisterManager.SKILL_KEY3.isDown()) {
            ItemStack itemStack = PlayerDataManager.getItemStack(null, PlayerDataType.SKILL_SLOT3);
            if (!itemStack.isEmpty() && itemStack.getItem() instanceof SkillBookType) {
                SkillBookType skill = (SkillBookType) itemStack.getItem();
                if (SkillUtils.canCastSkill(Minecraft.getInstance().player, skill)) {
                    NetWorkRegister.INSTANCE.sendToServer(new SkillCastPacket(PlayerDataType.SKILL_SLOT3.getName()));
                    skill.keyDown(Minecraft.getInstance().player);
                }
            }
        }
    }

    public static void callSkill(ServerPlayerEntity serverPlayerEntity, String slotName) {
        ItemStack itemStack = ItemStack.EMPTY;
        if (PlayerDataType.SKILL_SLOT1.getName().equals(slotName)) {
            itemStack = PlayerDataManager.getItemStack(serverPlayerEntity, PlayerDataType.SKILL_SLOT1);
        } else if (PlayerDataType.SKILL_SLOT2.getName().equals(slotName)) {
            itemStack = PlayerDataManager.getItemStack(serverPlayerEntity, PlayerDataType.SKILL_SLOT2);
        } else if (PlayerDataType.SKILL_SLOT3.getName().equals(slotName)) {
            itemStack = PlayerDataManager.getItemStack(serverPlayerEntity, PlayerDataType.SKILL_SLOT3);
        }

        SkillBookType skill = (SkillBookType) itemStack.getItem();
        if (!SkillUtils.canCastSkill(serverPlayerEntity, skill)) {
            return;
        }
        skill.keyDown(serverPlayerEntity);
    }

}
